.TH std::counted_iterator::operator[] 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::counted_iterator::operator[] \- std::counted_iterator::operator[]

.SH Synopsis
   constexpr decltype(auto) operator[]( std::iter_difference_t<I> n )
   const                                                                  \fI(since C++20)\fP
       requires std::random_access_iterator<I>;

   Accesses the element at specified relative location. The behavior is undefined if n
   is not less than the recorded distance to the end.

.SH Parameters

   n - position relative to current location

.SH Return value

   this->base()[n]

.SH Example


// Run this code

 #include <array>
 #include <iostream>
 #include <iterator>
 #include <list>

 int main()
 {
     std::array array{'A', 'B', 'C', 'D', 'E'};

     std::counted_iterator it{array.begin() + 1, /*count:*/ 3};

     for (int i{}; i != it.count(); ++i)
         std::cout << it[i] << ' ';
     std::cout << '\\n';

     for (int i{}; i != it.count(); ++i)
         it[i] += ('E' - 'A');

     for (int i{}; i != it.count(); ++i)
         std::cout << it[i] << ' ';
     std::cout << '\\n';

     std::list list{'X', 'Y', 'Z', 'W'};
     std::counted_iterator it2{list.begin(), 3};
 //  char x = it2[0]; // Error: requirement `random_access_iterator` was not satisfied.
     std::cout << *it2 << '\\n'; // OK
 }

.SH Output:

 B C D
 F G H
 X

.SH See also

   operator*  accesses the pointed-to element
   operator-> \fI(public member function)\fP
   (C++20)
